/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

/*
 * Copyright (c)  .
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * Email: owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 */

package com.aphrodite.ui.widgets;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.aphrodite.util.geom.GeomUtil;

/**
 * Icon strip menu
 * <p>
 * The PeepingIconsStrip is a strip of icons that appear to be hiding. Each icon
 * is full visible when it gains focus.<br/> <img
 * src="../../../resources/peep.png"><br/> There is no scrolling all the given
 * icons are displayed.
 * </p>
 * 
 * @author kentelt
 * 
 */
public class PeepingIconStrip extends TraversableWidget {

	private int currentIndex;
	private final Vector data;
	private final byte pad = 10;

	public PeepingIconStrip(Theme theme) {
		super(theme, 0, 0);
		data = new Vector();
		updateModel();
	}

	public void addIcon(Image icon) {
		data.addElement(icon);
		updateModel();
		reDrawWidget();
	}

	public void cleanUp() {
		// icons = null;
		data.removeAllElements();
	}

	// public void setIcons(Image[] icons) {
	// this.icons = icons;
	// setDims();
	// reDrawWidget();
	// stateChanged(this);
	// }

	private void decreaseIndex() {
		if (currentIndex > 0) {
			currentIndex--;
		} else {
			currentIndex = data.size() - 1;
		}
	}

	public int getSelectedIndex() {
		return currentIndex;
	}

	private void increaseIndex() {
		if (currentIndex < data.size() - 1) {
			currentIndex++;
		} else {
			currentIndex = 0;
		}
	}

	public boolean moveLeft() {
		decreaseIndex();
		reDrawWidget();
		return true;
	}

	public boolean moveRight() {
		increaseIndex();
		reDrawWidget();
		return true;
	}

	public boolean pointerPress(int px, int py) {
		if (isVisible && GeomUtil.withinBounds(px, py, x, y, width, height)) {
			int contextX = px - x;
			int contextY = py - y;
			int currWidth = pad;
			Image tmp = null;
			for (int i = 0; i < data.size(); i++) {
				tmp = (Image) data.elementAt(i);
				if (GeomUtil.withinBounds(contextX, contextY, currWidth, 0, tmp.getWidth(), tmp.getHeight())) {
					currentIndex = i;
					reDrawWidget();
					super.fire();
					break;
				}
				currWidth += tmp.getWidth() + pad;
			}
			return true;
		}
		return false;

	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		int iconX = pad;
		Image curr = null;
		for (int i = 0; i < data.size(); i++) {
			curr = (Image) data.elementAt(i);
			if (i == currentIndex) {
				g.drawImage(curr, iconX, height - curr.getHeight(), 0);
			} else {
				g.drawImage(curr, iconX, height - curr.getHeight() / 2, 0);
			}
			iconX += curr.getWidth() + pad;
		}
		release(g, width, height, true);

	}

	/**
	 * 
	 * @param index
	 * @throws IllegalArgumentException
	 *             if the index is <0 or if index >menu icons.length-1
	 */
	public void setSelectedIndex(int index) {
		if (index < 0 || index > data.size() - 1) {
			throw new IllegalArgumentException();
		}
		currentIndex = index;
		reDrawWidget();
		stateChanged(this);
	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		switch (dir) {
		case Canvas.UP:
			if (location == LOWER) {
				location = IN;
			} else {
				location = UPPER;
				return false;
			}
			break;
		case Canvas.DOWN:
			if (location == UPPER) {
				location = IN;
			} else {
				location = LOWER;
				return false;
			}
			break;
		case Canvas.LEFT:
			if (location == IN)
				moveLeft();
			break;
		case Canvas.RIGHT:
			if (location == IN)
				moveRight();
		}
		return false;
	}

	protected final void updateModel() {
		if (data.size() > 0) {
			width = pad;
			int tallest = ((Image) data.elementAt(0)).getHeight();
			Image tmp = null;
			for (int i = 0; i < data.size(); i++) {
				tmp = (Image) data.elementAt(i);
				width += tmp.getWidth() + pad;
				if (tmp.getHeight() > tallest) {
					tallest = tmp.getHeight();
				}
			}
			this.height = tallest;
		} else {
			this.height = 0;
		}

	}
}
